package com.jic.manager.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.manager.exception.AppRuntimeException;
import com.jic.member.constant.consist.ResponseCode;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSONObject;

import java.util.List;
import javax.annotation.Resource;
import com.jic.manager.model.entity.SysProtocol;
import com.jic.manager.mapper.SysProtocolMapper;
import com.jic.manager.service.SysProtocolService;
import lombok.extern.slf4j.Slf4j;


@Service
@Slf4j
public class SysProtocolServiceImpl implements SysProtocolService {

	@Resource
	private SysProtocolMapper mapper;


	/**
	 * 新增记录
	 */
	@Override
	public int save(SysProtocol request) {
	 int flag = mapper.insert(request);
	 if (flag == 1) {
	   return 1;
	 }else {
	   throw new AppRuntimeException(ResponseCode.EXCEPTION, "新增失败");
	 }
	}


	/**
	 * 保存或修改记录信息
	 */
	@Override
	public int saveOrUpdate(SysProtocol request){
		log.info("保存或修改记录信息，入参 = {}", JSONObject.toJSONString(request));
		try {
			if (request == null) {
				throw new AppRuntimeException(ResponseCode.EXCEPTION,"入参为空");
			}
			if (request.getId() != null) {
				log.info("保存或修改记录信息，修改，入参 = {}", JSONObject.toJSONString(request));
				int flag = mapper.updateByPrimaryKey(request);
				if(flag == 1){
				  return 1;
				}
			}else{
				log.info("保存或修改记录信息，保存，入参 = {}", JSONObject.toJSONString(request));
				int flag = mapper.insertSelective(request);
				if(flag == 1){
				  return 1;
				}else{
				  throw new AppRuntimeException(ResponseCode.EXCEPTION,"修改失败");
				}
			}
		} catch (Exception e) {
			log.error("用户保存或修改失败", e);
			throw new AppRuntimeException(ResponseCode.EXCEPTION,"用户保存或修改失败");
		}
	     return -1;
	}

	/**
	 * 删除记录
	 */
	@Override
	public int deleteByPrimaryKey(SysProtocol request) {
		int flag = mapper.deleteByPrimaryKey(request);
	    if (flag == 1) {
	      return 1;
	    }else {
	      throw new AppRuntimeException(ResponseCode.EXCEPTION, "删除失败");
	    }
	}


	/**
	 * 逻辑删除记录
	 */
	@Override
	public int deleteByPrimaryKeyLogically(SysProtocol request) {
		request.setDeleteFlag(1);
		int flag = mapper.updateByPrimaryKey(request);
	    if (flag == 1) {
	      return 1;
	    }else {
	      throw new AppRuntimeException(ResponseCode.EXCEPTION, "逻辑删除记录失败");
	    }
	}


	/**
	 * 修改
	 */
	@Override
	public int updateByPrimaryKeySelective(SysProtocol request){
		try {
			log.info("修改记录");
			SysProtocol param = new SysProtocol();
			BeanUtils.copyProperties(request, param);
			int flag = mapper.updateByPrimaryKeySelective(param);
	        if (flag == 1) {
	         return 1;
	        }else {
	          throw new AppRuntimeException(ResponseCode.EXCEPTION, "修改记录失败");
	        }
		} catch (Exception e) {
			log.error("修改记录失败", e);
			throw new AppRuntimeException(ResponseCode.EXCEPTION,"修改记录失败");
		}

	}

	/**
	 * 记录详情
	 */
	@Override
	public SysProtocol getByPrimaryKey(SysProtocol request) {
		return mapper.selectByPrimaryKey(request);
	}


	/**
	 * 分页查询
	 */
	@Override
	public PageInfo<SysProtocol> listPageBySelective(SysProtocol request, int pageNum, int pageSize) {
	PageHelper.startPage(pageNum, pageSize);
	PageHelper.orderBy(" create_time desc");
	List<SysProtocol> list = mapper.listPageBySelective(request);
	PageInfo pageInfo = new PageInfo(list);
	PageResult<SysProtocol> result = new PageResult<>();
	result.setTotal(pageInfo.getTotal());
	result.setRows(list);
	return new PageInfo(list);
	}

	/**
	 * 不分页查询
	 */
	@Override
	public List<SysProtocol> listAllRecord(SysProtocol request) {
		//分页处理
		request.setDeleteFlag(0);
		return mapper.selectBySelective(request);
	}

	/*
	    启用
	 */

    @Override
    public Integer updatestartUsing(SysProtocol param) {
        log.info("修改信息，入参 = {}", JSONObject.toJSONString(param));
        if (param.getId() == null){
            throw new AppRuntimeException(ResponseCode.EXCEPTION, "传入参数为空");
        }
        Integer up = mapper.updatestartUsing(param);
	    if (up == 1){
	        return 1;
        }

        return 0;
    }

    /*
	    停用
	 */

    @Override
    public Integer updateblockUp(SysProtocol param) {
        log.info("修改信息，入参 = {}", JSONObject.toJSONString(param));
        if (param.getId() == null){
            throw new AppRuntimeException(ResponseCode.EXCEPTION, "传入参数为空");
        }
        Integer up = mapper.updateblockUp(param);
	    if (up == 1){
	        return 1;
        }

        return 0;
    }

    /*
        根据类型查询详情
     */
    @Override
    public List<SysProtocol> pageList(SysProtocol param) {
		List<SysProtocol> list =  mapper.pageList(param);
        return list;
    }

    /*
        根据ID查询详情
     */

    @Override
    public SysProtocol pageId(SysProtocol param) {
        if (param.getId() == null){
            throw new AppRuntimeException(ResponseCode.EXCEPTION, "传入的ID为空");
        }
        SysProtocol list =  mapper.selectByPrimaryKey(param);
        return list;
    }

    @Override
    public Integer updateAll(Long id) {
        if (id == null){
            throw new AppRuntimeException(ResponseCode.EXCEPTION, "传入的ID为空");
        }
        Integer all = mapper.updateAll(id);
        if (all != 1){
            throw new AppRuntimeException(ResponseCode.EXCEPTION, "操作失败");
        }
        return 1;
    }

}
